package com.vallis.lotteryonline.controller;


import com.vallis.lotteryonline.annotation.JwtIgnore;
import com.vallis.lotteryonline.entity.Audience;
import com.vallis.lotteryonline.entity.User;
import com.vallis.lotteryonline.service.UserService;
import com.vallis.lotteryonline.util.JwtTokenUtil;
import com.vallis.lotteryonline.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-07-13
 */
@RestController
@CrossOrigin
@RequestMapping("/user/")
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private Audience audience;

    @Autowired
    UserService userService;
    
    @PostMapping(value = "ttt")
    public @ResponseBody
    Result ttt(){
       return Result.error("密码不能为空");
    }
    
    /**
     * 客户注册，密码MD5加密，最好有身份证号
     */
    @PostMapping(value = "register")
    @JwtIgnore
    public @ResponseBody
    Result register(
            
            //@RequestParam Map<String, String> params
    ) {
        /*
        此处注意-> 前端传Map params->
        params
        {
        user_phone： 手机号 -》String
        user_password:密码 -》String
        invite_code：邀请码-》String
        }
         */
        
        Map<String, String> params=new HashMap<>();
        
        if (params.isEmpty()) {
            return Result.error("参数为空");
        }
        String user_phone = params.get("user_phone");
        String user_password = params.get("user_password");
        if (StringUtils.isEmpty(user_phone)) {
            return Result.error("手机号不能为空");
        }
        String phoneCheck = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        if (!user_phone.matches(phoneCheck)) {
            return Result.error("手机号不正确");
        }
        Map<String, Object> phoneMap = new HashMap<>();
        phoneMap.put("user_phone", user_phone);
        List<User> list = userService.listByMap(phoneMap);
        if (list != null && list.size() > 0) {
            return Result.error("客户已存在");
        }
        if (StringUtils.isEmpty(user_password)) {
            return Result.error("密码不能为空");
        }
        String passwordMd5 = DigestUtils.md5DigestAsHex(user_password.getBytes());

        User user = new User();
        user.setUserPhone(user_phone);
        user.setUserPassword(passwordMd5);
        user.setUserPayAmont(0);
        user.setUserRegisterTime(new Date());
        if (!userService.save(user)) {
            return Result.error("注册失败");
        }
        ;
        String token = JwtTokenUtil.createJWT(user.getId(), audience);
        return Result.ok(token);


    }

    /**
     * 客户登录
     */
    @PostMapping(value = "login")
    @JwtIgnore
    public @ResponseBody
    Result login(@RequestParam Map<String, String> params) {
        logger.info("login=" + params.toString());
        if (params.isEmpty()) {
            return Result.error("参数为空");
        }
        String phone = params.get("user_phone");
        String password = params.get("user_password");
        if (StringUtils.isEmpty(phone)) {
            return Result.error("手机号不能为空");
        }
        String phoneCheck = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        if (!phone.matches(phoneCheck)) {
            return Result.error("手机号不正确");
        }
        if (StringUtils.isEmpty(password)) {
            return Result.error("密码不能为空");
        }
        String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());

        Map<String, Object> phoneMap = new HashMap<>();
        phoneMap.put("user_phone", phone);
        List<User> list = userService.listByMap(phoneMap);
        if (list == null || list.size() == 0) {
            return Result.error("客户未注册,请先注册");
        }
        if (list.size() > 1) {
            return Result.error("该手机号已被多客户使用");
        }
        User user = list.get(0);
        if (passwordMd5.equals(user.getUserPassword())) {
            String token = JwtTokenUtil.createJWT(user.getId(), audience);
            return Result.ok(token);
        } else {
            return Result.error("密码错误");
        }
    }

}
